//
//  XMCircelProgressView.swift
//  sinaDemo
//
//  Created by Xueming on 2021/3/31.
//

import UIKit

class XMCircelProgressView: UIView {
    var progress:CGFloat = 0.0 {
        didSet{
            DispatchQueue.main.async {
                self.setNeedsDisplay()
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.init(white: 0.5, alpha: 0.5)
        layer.masksToBounds = true
        layer.cornerRadius = frame.width * 0.5
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func draw(_ rect: CGRect) {
        // Drawing code
        super.draw(rect)
        
        let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
        let radius = rect.width * 0.5 - 3
        let startAngle = CGFloat( -Double.pi * 0.5)
        let endAngle = CGFloat(-Double.pi * 0.5 +  2 * Double.pi * Double(progress))
        
        let bezerPath = UIBezierPath()
        bezerPath.lineWidth = 1
        bezerPath.addArc(withCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle , clockwise: true)
        bezerPath.addLine(to: center)
        bezerPath.close()
        UIColor.init(white: 1.0, alpha: 0.9).setFill()
        
        bezerPath.fill()
    }
    @objc func testrun(){
        progress += 0.05
    }
}
